package menu

import (
	"context"
	"go-zero-project/services/user/rpc/pb"

	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SelectListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSelectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SelectListLogic {
	return &SelectListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SelectListLogic) SelectList() (resp *types.MenuSelectResp, err error) {
	// 1.builder rpc request
	emptyObj := pb.EmptyObj{}
	// 2.request rpc
	menuSelectResp, err := l.svcCtx.UserCenterClient.MenuSelect(l.ctx, &emptyObj)
	if err != nil {
		return nil, err
	}
	// 3.return obj
	menuSelectArr := menuSelectResp.MenuArr
	var selectArr []types.MenuInfoResp
	if len(menuSelectArr) == 0 {
		return &types.MenuSelectResp{MenuArr: selectArr}, err
	}
	// 3.1 递归菜单那
	navigationLogic := NewNavigationLogic(l.ctx, l.svcCtx)
	loopMenuArr := navigationLogic.loopMenuArr(menuSelectArr)
	return &types.MenuSelectResp{
		MenuArr: loopMenuArr,
	}, err
}
